package com.lzq.chatroom_project.common.exception;

import com.lzq.chatroom_project.common.pojo.Result;
import com.lzq.chatroom_project.common.pojo.ResultCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@ControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    @ResponseBody
    @ExceptionHandler(ApplicationException.class)
    public Result applicationExceptionHandler(ApplicationException e) {
        //打印异常信息
        e.printStackTrace();
        // 打印日志
        log.error(e.getMessage());
        if(e.getErrorResult()!=null) {
            return e.getErrorResult();
        }
        //非空校验
        if(e.getMessage()==null ||e.getMessage().equals("")) {
            return Result.failed(ResultCode.ERROR_SERVICES);
        }
        //返回具体的异常信息
        return Result.failed(e.getMessage());
    }

    @ResponseBody
    @ExceptionHandler(Exception.class)
    public Result exceptionHandler(Exception e) {
        //打印异常信息
        e.printStackTrace();
        // 打印日志
        log.error(e.getMessage());
        //非空校验
        if(e.getMessage()==null ||e.getMessage().equals("")) {
            return Result.failed(ResultCode.ERROR_SERVICES);
        }
        //返回具体的异常信息
        return Result.failed(e.getMessage());
    }
}